package com.bjsxt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.SysMenu;
import com.bjsxt.domain.SysPrivilege;
import com.bjsxt.domain.SysRolePrivilege;
import com.bjsxt.mapper.SysRolePrivilegeMapper;
import com.bjsxt.model.RolePrivilegeParam;
import com.bjsxt.service.SysMenuService;
import com.bjsxt.service.SysPrivilegeService;
import com.bjsxt.service.SysRolePrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService{
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysPrivilegeService sysPrivilegeService;
    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;
    @Override
    public List<SysMenu> findSysMenuAndPrivileges(Long roleId) {
        //我们在页面显示的是一个二级菜单，以及二级菜单包含的权限
        List<SysMenu> list = sysMenuService.list() ;
        if(CollectionUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        List<SysMenu> rootMenus = list.stream()
                .filter(sysMenu -> sysMenu.getParentId() == null)
                .collect(Collectors.toList());//查询一级菜单
        if(CollectionUtil.isEmpty(rootMenus)){
            return Collections.emptyList();
        }
        ArrayList<SysMenu> sysMenus = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            sysMenus.addAll(getChildMenus(sysMenu.getId(),roleId,list));
        }
        return sysMenus;
    }



    /**
     * 查询菜单的子菜单
     * @param parentId 父菜单的ID
     * @param roleId    当前查询的角色的ID
     * @param sources
     * @return
     */
    private List<SysMenu> getChildMenus(Long parentId, Long roleId, List<SysMenu> sources) {
        ArrayList<SysMenu> childs = new ArrayList<>();
        for (SysMenu source : sources) {
            if(source.getParentId() == parentId){
                childs.add(source);
                source.setChilds(getChildMenus(source.getId(),roleId,sources));
                List<SysPrivilege>  sysPrivileges = sysPrivilegeService.getAllSysPrivilege(source.getId(),roleId);
                source.setPrivileges(sysPrivileges);

            }

        }
        return childs;

    }

    @Override
    @Transactional
    public boolean grantPrivilege(RolePrivilegeParam rolePrivilegeParam) {
        Long roleId = rolePrivilegeParam.getRoleId();//角色ID
        //删除之前该角色的权限
        sysRolePrivilegeService.removeById(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getRoleId, roleId));

            //一处之前的值成功
            List<Long> privilegeIds = rolePrivilegeParam.getPrivilegeIds();
            if(!CollectionUtil.isEmpty(privilegeIds)){
                List<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
                for (Long privilegeId : privilegeIds) {
                    SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                    sysRolePrivilege.setRoleId(rolePrivilegeParam.getRoleId());
                    sysRolePrivilege.setPrivilegeId(privilegeId);
                    sysRolePrivileges.add(sysRolePrivilege);
                }
                //新增该角色的权限
                return sysRolePrivilegeService.saveBatch(sysRolePrivileges);
            }

        return true;
    }

}


